import {
    statusBarHeight,
    navbarHeight,
    navbarScreenWidth,
    topHeight,
    bottomSafeHeight,
    screenHeight,
    clientRect
} from "./size.uitl";
import {deepExpand} from "./utils.utils";


const winSize = {
    "status-bar-height": statusBarHeight,
    "navBar-height": navbarHeight(),
    "navBar-screen-width": navbarScreenWidth(),
    "top-height": topHeight(),
    "bottom-safe-height": bottomSafeHeight(),
    "screen-height": screenHeight(true),
    "rect": clientRect(),
    "tabBar-height": 60,
};


// 辅助函数：RGB转HSL
function rgbToHsl(r, g, b) {
    r /= 255;
    g /= 255;
    b /= 255;

    var max = Math.max(r, g, b);
    var min = Math.min(r, g, b);
    var h, s, l = (max + min) / 2;

    if (max === min) {
        h = s = 0; // achromatic
    } else {
        var d = max - min;
        s = l > 0.5 ? d / (2 - max - min) : d / (max + min);

        switch (max) {
            case r: h = (g - b) / d + (g < b ? 6 : 0); break;
            case g: h = (b - r) / d + 2; break;
            case b: h = (r - g) / d + 4; break;
        }

        h /= 6;
    }

    return { h: h, s: s, l: l };
}

// 辅助函数：HSL转RGB
function hslToRgb(h, s, l) {
    var r, g, b;

    if (s === 0) {
        r = g = b = l; // achromatic
    } else {
        function hue2rgb(p, q, t) {
            if (t < 0) t += 1;
            if (t > 1) t -= 1;
            if (t < 1/6) return p + (q - p) * 6 * t;
            if (t < 1/2) return q;
            if (t < 2/3) return p + (q - p) * (2/3 - t) * 6;
            return p;
        }

        var q = l < 0.5 ? l * (1 + s) : l + s - l * s;
        var p = 2 * l - q;

        r = hue2rgb(p, q, h + 1/3);
        g = hue2rgb(p, q, h);
        b = hue2rgb(p, q, h - 1/3);
    }

    return {
        r: Math.round(r * 255),
        g: Math.round(g * 255),
        b: Math.round(b * 255)
    };
}

// 辅助函数：十六进制颜色转RGB
function hexToRgb(hex) {
    // 移除#符号
    hex = hex.replace('#', '');
    // 截取6位
    if (hex.length > 6) {
        hex = hex.substr(0, 6);
    }
    // 处理3位和6位十六进制颜色值
    if (hex.length === 3) {
        hex = hex.split('').map(function(char) {
            return char + char;
        }).join('');
    }
    var bigint = parseInt(hex, 16);
    return {
        r: (bigint >> 16) & 255,
        g: (bigint >> 8) & 255,
        b: bigint & 255
    };
}

function hexToRgba(hex,  alpha) {
    var rgb = hexToRgb(hex);
    return 'rgba(' + rgb.r + ',' + rgb.g + ',' + rgb.b + ',' + alpha + ')';
}

// 辅助函数：RGB转十六进制颜色
function rgbToHex(r, g, b) {
    return '#' + toHex(r) + toHex(g) + toHex(b);
}

function toHex(n) {
    var hex = n.toString(16).slice(1);
    return hex.length === 1 ? '0' + hex : hex;
}

// 单文字转十六进制
function hexColor (char) {
    return '#' + char + char+ char + char + char + char;
}

function covertTheme(style){
    let str = `--win-font: ${style.font}rpx;--win-font-small:${style.font * 0.6}rpx;--win-font-large:${style.font * 1.4}rpx;`;
    const winSizeStyle = deepExpand(winSize, "--win", "-");
    for (const key in winSizeStyle) {
        str += `${key}: ${winSizeStyle[key]}px;`;
    }
    const themeStyle = deepExpand(style, "--win", "-");
    for (const key in themeStyle) {
        if (key.indexOf("font") > -1){
            continue;
        }
        str += `${key}: ${themeStyle[key]};`;
    }
    return str;
}

function covertMask(main) {
    return 'width:100%;height:100%;background-color: ' + main + ' !important; opacity: 0.5;'
}

module.exports = {
    covertTheme,
    covertMask,
}